<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    
	
	public function test(){
		$calballUlr = \Org\Net\Fun::url('home/index/callbackTooken');
//      $goUrl = 'http://res.help2x.com/index.php/home/authz?appid=xijiu&publickey=StaMOsUF7i&redirect_uri='.$calballUlr;
        $goUrl = 'http://wechat.bluej.cn/index.php/home/authz?appid=bluej&publickey=8Y75No0rcq&redirect_uri='.$calballUlr;    
		
		//测试数据 begin
        $id = I("get.id");
        $id = intval($id);
        if($id<20 && $id>0){
            $userModel = M('User');
			$whereTouser = array();
			$whereTouser['id'] = $id;
			$userData = $userModel->where($whereTouser)->find();
			if(empty($userData)){
				echo 'user is not';exit;
			}
			$sessionData = array();
			$sessionData['wx_id'] = $userData['wx_id'];
			$sessionData['image'] = $userData['image'];
			$sessionData['name'] = $userData['name'];
			session('userInfo',$sessionData);
        }
        //测试数据 end
        
        $userInfo = session('userInfo');
        
        //判断是否登陆
        if($userInfo){
            $goUrl = \Org\Net\Fun::url('home/page/index');
        }
        //判断是关注
        if(empty($userInfo['wx_id']) || empty($userInfo['image']) && empty($userInfo['name'])){
            //未关注操作
        }
        
        header("Location:".$goUrl);
	}




	/**
     * 入口判断是否已经授权登陆
     */
    public function index(){
        //exit;
//		header("Content-Type:text/html;charset=utf-8"); 
//		echo '正在制作中，敬请期待';exit;
//		
//        $calballUlr = \Org\Net\Fun::url('home/index/callbackTooken');
//        $goUrl = 'http://res.help2x.com/index.php/home/authz?appid=xijiu&publickey=StaMOsUF7i&redirect_uri='.$calballUlr;
//        
//		//测试数据 begin
//        $id = I("get.id");
//        $id = intval($id);
//        if($id<8 && $id>0){
//            $userModel = M('User');
//			$whereTouser = array();
//			$whereTouser['id'] = $id;
//			$userData = $userModel->where($whereTouser)->find();
//			if(empty($userData)){
//				echo 'user is not';exit;
//			}
//			$sessionData = array();
//			$sessionData['wx_id'] = $userData['wx_id'];
//			$sessionData['image'] = $userData['image'];
//			$sessionData['name'] = $userData['name'];
//			
//			session('userInfo',$sessionData);
//        }
//        //测试数据 end
//        
//        
//        $userInfo = session('userInfo');
//        
//        
//        //判断是否登陆
//        if($userInfo){
//            $goUrl = \Org\Net\Fun::url('home/page/index');
//        }
//        //判断是关注
//        if(empty($userInfo['wx_id']) || empty($userInfo['image']) && empty($userInfo['name'])){
//            //未关注操作
//        }
//        
//        header("Location:".$goUrl);
		
		
		$calballUlr = \Org\Net\Fun::url('home/index/callbackTooken');
//      $goUrl = 'http://res.help2x.com/index.php/home/authz?appid=xijiu&publickey=StaMOsUF7i&redirect_uri='.$calballUlr;
        $goUrl = 'http://wechat.bluej.cn/index.php/home/authz?appid=bluej&publickey=8Y75No0rcq&redirect_uri='.$calballUlr;  
        
        $userInfo = session('userInfo');
        
        //判断是否登陆
        if($userInfo){
            $goUrl = \Org\Net\Fun::url('home/page/index');
        }
        //判断是关注
        if(empty($userInfo['wx_id']) || empty($userInfo['image']) && empty($userInfo['name'])){
            //未关注操作
        }
        header("Location:".$goUrl);exit;
    }
    
    /**
     * 回调tooken
     */
    public function callbackTooken(){
		$access_token = $_REQUEST['access_token'];
		$openid = $_REQUEST['openid'];
		$result = file_get_contents("https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN");
		$data = json_decode($result, true);
		
		$wx_id = $data['openid'];
		$headimgurl = $data['headimgurl'];
		$nickname = $data['nickname'];
		
        $sessionData = array();
        $sessionData['wx_id'] = $wx_id;
        $sessionData['image'] = $headimgurl;
        $sessionData['name'] = $nickname;
        
        if(empty($wx_id)||empty($headimgurl)||empty($nickname)){
            $gotourl = \Org\Net\Fun::url('home/index/test');
            header("Location:".$gotourl);exit;
        }
        
        
		$userModel = M('User');
		$userInfoData = $userModel->where("wx_id='{$wx_id}'")->find();
		if(!$userInfoData){
			$userData = array();
			$userData['wx_id'] = $wx_id;
			$userData['image'] = $headimgurl;
			$userData['name'] = $nickname;
			$userData['add_time'] = \Org\Net\Fun::now_time();
			$res = $userModel->add($userData);
        }
        session('userInfo',$sessionData);
        
        //如果有回调链接
        $callUrl = session('callUrl');
        if($callUrl){
            session('callUrl',"");
            $goUrl = $callUrl;
        } else {
            $goUrl = \Org\Net\Fun::url('home/page/index');
        }
        header("Location:".$goUrl);
    }
    
    
   
}